Page({

  /**
   * 页面的初始数据
   */
  data: {
    schema: [
      {
        label:'滚动到页面的目标位置，单位 px',
        prop:'scrollTop',
        type:'number'
      },
      {
        label:'滚动动画的时长，单位 ms',
        prop:'duration',
        type:'number'
      },
      {
        label:'选择器',
        prop:'selector',
        type:'picker',
        labelOptions:['空', '#the-id1', '#the-id2', '#the-id3'], //选项
        valueOptions:['', '#the-id1', '#the-id2', '#the-id3'], //选项对应的值
        value: ''
      },
      {
        label:'偏移距离, 需要和 selector 参数搭配使用，可以滚动到 selector 加偏移距离的位置，单位 px',
        prop:'offsetTop',
        type:'number'
      }
    ],
    formData: {
      duration: 300,
      selector: '#the-id1'
    },
    title: '',
    result: '',
    remark: '',
    webUrl: '',
    apiName: ''
  },
  onLoad(options) {
    console.log('options', options)
    wx.setNavigationBarTitle({
      title: options.title,
    })

    this.setData({
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
  },
  getFormData(e) {
    const formData = {}
    this.data.schema.forEach(item => {
      if(item.type === 'number') {
        if(!isNaN(+e.detail.formData[item.prop]) && e.detail.formData[item.prop] !== '') {
          formData[item.prop] = +e.detail.formData[item.prop]
        }
      } else {
        formData[item.prop] = e.detail.formData[item.prop]
      }
    })
    this.setData({
      formData: formData
    })
  },
  choose1(e) {
    const that = this;
    console.log(this.data.formData);
    wx.pageScrollTo({
      ...this.data.formData,
      success(res) {
        console.log('res', res);
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      fail(err) {
        console.log('err', err);
        that.setData({
          result: err,
          apiName: e.currentTarget.dataset.apiname
        })
      },
      complete(res) {
        console.log('res', res);
        that.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }
    })
  },
})